#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

############################################################################

# file_dir = r'/rootfs/media/kasim/Data/data/Erised/ricemee-age/Images'
# file_list_file = r'/rootfs/media/kasim/Data/data/Erised/ricemee-age/Annotations/bbox_gt.txt'
file_dir = r'/rootfs/media/kasim/Data/data/Erised/age-gender/Images'
file_list_file = r'/rootfs/media/kasim/Data/data/Erised/age-gender/Annotations/bbox_gt.txt'

file_count = 0
out_file_path = os.path.join(os.path.dirname(file_list_file), 'file_list.txt')
out_file = open(out_file_path, 'w', encoding='utf-8')
with open(file_list_file, 'r', encoding='utf-8') as file:
    while True:
        line = file.readline()
        img_path = line.strip()
        if not img_path:  # To end of file
            break
        n_bboxs = int(file.readline().strip())
        for _ in range(n_bboxs):
            file.readline()

        image_path = os.path.join(file_dir.encode('utf-8'), img_path.encode('utf-8'))
        if not os.path.exists(image_path):
            continue

        out_file.write(line)
        file_count += 1
        if file_count % 100 == 0:
            print('Process File Count:', file_count)

out_file.close()
os.system('chmod a+wr {}'.format(out_file_path))
print('Process File Count:', file_count)

print('Finish!')